import _ from 'lodash'
import Levels from './rule-level'
import Types from './rule-type'
import Utils from './_utils'

const rule = 'required'

/*
* config {
*   required: undefined // Function/Boolean
* }
* */

export default {
  priority: Levels.HIGH_LEVEL,
  validate: function (value, config, model, formModel, filed, fieldScope) {
    value = _.isNil(value) ? '' : _.trim(value.toString())
    return {
      valid: Utils.validateWrapper(config.required, true, value.length, config, model, formModel, (params, value, model, formModel) => {
        return !params || value > 0
      }),
      errorTemplate: undefined,
      errorParams: undefined
    }
  },
  rule: rule,
  type: Types.ATTRIBUTE
}
